package com.pucheng.bracelet.ui.activity.watch

import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothGatt
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.KeyEvent
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.clj.fastble.BleManager
import com.clj.fastble.callback.BleGattCallback
import com.clj.fastble.data.BleDevice
import com.clj.fastble.exception.BleException
import com.pucheng.bracelet.R
import com.pucheng.bracelet.app.base.BaseTitleActivity
import com.pucheng.bracelet.app.util.CacheUtil
import com.pucheng.bracelet.data.bean.WatchInfoBean
import com.pucheng.bracelet.databinding.ActivityWatchScanBinding
import com.pucheng.bracelet.ui.adapter.WatchInfoAdapter
import com.pucheng.bracelet.viewmodel.WatchScanViewModel
import kotlinx.android.synthetic.main.activity_watch_scan.*
import kotlinx.android.synthetic.main.layout_title.*
import kotlin.concurrent.thread

class WatchScanActivity : BaseTitleActivity<WatchScanViewModel, ActivityWatchScanBinding>() {
    private var isFinsh: Boolean = false

    override fun layoutId(): Int = R.layout.activity_watch_scan
    override fun initView(savedInstanceState: Bundle?) {
        ibtn_back.setOnClickListener {
            finish()
            mViewModel.stopScan()
            isFinsh = true
        }
        rv_scan_band.layoutManager = LinearLayoutManager(this)
        rv_scan_band.adapter = mViewModel.bleDervicesAdp
        mViewModel.searchDevice()
        mViewModel.bleDervicesAdp.setOnItemClickListener { adapter, _, position ->
            mViewModel.connectBle((adapter.getItem(position) as BleDevice).mac)
        }
        if (!BleManager.getInstance().isBlueEnable) {
            onEnableBluetoothClicked()
        }
    }

    fun onEnableBluetoothClicked() {
        val enableIntent =
            Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
        startActivity(enableIntent)
    }

    override fun createObserver() {
        mViewModel.apply {
            connectedWatchInfo.observe(this@WatchScanActivity, Observer {
                if (it != null && !TextUtils.isEmpty(it.mac)) {
                    appViewModel.watchInfo.value = it
                    appViewModel.bleConnectStatus.value = true
                    finish()
                }
            })
            isScanFinished.observe(this@WatchScanActivity, Observer {
                if (it && isFinsh) {
                    finish()
                }
            })
        }
    }

    override fun titleText(): String = getString(R.string.add_device)

//    override fun onPause() {
//        super.onPause()
//        thread(true){
//            BleManager.getInstance().cancelScan()
//        }
//    }

    override fun onRestart() {
        super.onRestart()
        mViewModel.searchDevice()
    }


    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (mViewModel != null) {
                mViewModel.stopScan()
                isFinsh = true
            }
        }
        return super.onKeyDown(keyCode, event)
    }
}

